package cn.org.timespace.easyframe.util.sms.mobiles;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.util.sms.telecom.smsUtil.SMGPPropertie;

import com.jasson.im.api.APIClient;
import com.jasson.im.api.MOItem;
import com.jasson.im.api.RPTItem;

public class CMobileClient {
	
	private static final Log log =LogFactory.getLog(CMobileClient.class);
	private String host = "218.200.69.63";//218.200.69.63--192.168.1.139
	private String dbName = "mas";
	private String apiId = "szapi";
	private String name = "szapi";
	private String pwd = "szapi";
	private IMessageStatus msgStatus;	
	private long timestemp=3000;
	private boolean isRun =false;
	private APIClient handler = new APIClient();
	private Runnable moRun= new Runnable() {		
		public void run(){
			try{
				 while(isRun)
				 {
				 	recvSM();//�����
					Thread.sleep(timestemp);
				 }
				} catch (InterruptedException e) {
					e.printStackTrace();
			}
		}
	};
	
	private Runnable rptRun = new Runnable() {		
		public void run(){			
			 try {
				 while(isRun)
				 {
					 recvRPT();	//�� ���ͱ������
					 Thread.sleep(timestemp);
				 }
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	};
	
	private Thread moThread;
	private Thread rptThread;
	
	public CMobileClient()
	{
		
	}
	
	public boolean init(){
		int connectRe= handler.init(host, name, pwd, apiId,dbName);
		
        if(connectRe == APIClient.IMAPI_SUCC)
        {
        	log.info("��ʼ���ɹ�");
        	isRun=true;
        	boolean sendType = Boolean.parseBoolean(SMGPPropertie.getProperty("mobile.sendType").trim());// �Ƿ�����չ���
        	if(sendType){
        		moThread = new Thread(moRun);
	        	moThread.start();//�����
	        	
	        	rptThread = new Thread(rptRun);
	        	rptThread.start();//����ͱ������
        	}
        	return isRun;        	
        }
        else if(connectRe == APIClient.IMAPI_CONN_ERR)
        {
        	log.info("l��ʧ��");
        	return false;
        }
        else if(connectRe == APIClient.IMAPI_API_ERR)
        {
        	log.info("apiID������");
        	return false;
        }
        return false;
	}
	
	/**
	 * 
	 * ���ͽӿ�
	 * @param msg
	 */
	public int sendSM(AbstractMessage msg)
	{	
		int result = 0;
		if(msg !=null && msg.isWapPush())
		{
			MessagePush msg1 = (MessagePush)msg;
			try{
				result = handler.sendSM(msg1.tmpMobile, msg1.tmpContent, msg1.smID, Long.parseLong(msg1.tmpSrcID), msg1.Url);
			}catch (Exception e) {
				result=APIClient.IMAPI_CONN_ERR;
			}
		}else{
			Message  msg1 = (Message)msg;
			try{
				result = handler.sendSM(msg1.tmpMobile, msg1.tmpContent, msg1.smID , Long.parseLong(msg1.tmpSrcID));
			}catch (Exception e) {
				result=APIClient.IMAPI_CONN_ERR;
			}
		}        
        
		if(result == APIClient.IMAPI_SUCC)
        {            
            log.info("���ͳɹ�");
        }
        else if(result == APIClient.IMAPI_INIT_ERR)
        	log.info("δ��ʼ��");
        else if(result == APIClient.IMAPI_CONN_ERR)
        	log.info("��ݿ�l��ʧ��");
        else if(result == APIClient.IMAPI_DATA_ERR)
        	log.info("�������");
        else if(result == APIClient.IMAPI_DATA_TOOLONG)
        	log.info("��Ϣ����̫��");
        else if(result == APIClient.IMAPI_INS_ERR)
        	log.info("��ݿ�������");
        else
        	log.info("�����������");
        
        if(msgStatus!=null)
        {
        	msgStatus.SeneMessage(result,msg);
        }
        return result;
	}
	
	/**
	 * 
	 * @return
	 */
	public void recvRPT()
	{		
		log.info("ɨ��һ����recvRPT:");
		RPTItem[] rpts = handler.receiveRPT();
		if(rpts!=null&& rpts.length>0)
		{
			msgStatus.DeliverReport(rpts);
		}
	}
	
	
	
	/**
	 * 
	 * ��������
	 */
	public void recvSM()
	{
		log.info("ɨ��һ����MO:");
		MOItem[] mos = handler.receiveSM();
		if(mos!=null && mos.length>0)
		{
			msgStatus.Deliver(mos);
		}
	}

	
	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public String getApiId() {
		return apiId;
	}

	public void setApiId(String apiId) {
		this.apiId = apiId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	public boolean release()
	{
		log.info("��ֹMO�߳�");
		if(moThread!=null)
		{
			moThread.interrupt();
			moThread=null;
		}
		
		log.info("��ֹ״̬�����߳�");
		if(rptThread!=null)
		{
			rptThread.interrupt();
			rptThread =null;
		}
		return true;
	}

	public IMessageStatus getMsgStatus() {
		return msgStatus;
	}

	public void setMsgStatus(IMessageStatus msgStatus) {
		this.msgStatus = msgStatus;
	}

}
